﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Test;
using Test.Analysis;

namespace TestLucene
{
    class Program
    {
        static void Main(string[] args)
        {
            /// 建立索引
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriter writer = new IndexWriter("IndexDirectory", analyzer, true);
            AddDocument(writer, "SQL Server 2008 的发布", "SQL Server 2008 的新特性");
            AddDocument(writer, "ASP.Net MVC框架配置与分析", "而今，微软推出了新的MVC开发框架，也就是Microsoft ASP.NET 3.5 Extensions");
            writer.Optimize();
            writer.Close();

            ///查询索引
            IndexSearcher searcher = new IndexSearcher("IndexDirectory");
            MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[] { "title", "content" }, analyzer);
            Query query = parser.Parse("MVC");
            Hits hits = searcher.Search(query);

            for (int i = 0; i < hits.Length(); i++)
            {
                Document doc = hits.Doc(i);
                Console.WriteLine(string.Format("title:{0} content:{1}", doc.Get("title"), doc.Get("content")));
            }
            searcher.Close();



            ///简单词法测试
            SimpleAnalyzerTest  test=new SimpleAnalyzerTest();
            test.ReusableTokenStreamTest();

            AllAnalysisTest allTest = new AllAnalysisTest();
            allTest.TestMethod();
            Console.ReadKey();

        }

        static void AddDocument(IndexWriter writer, string title, string content)
        {
            Document document = new Document();
            document.Add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
            document.Add(new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED));
            writer.AddDocument(document);
        }
    }
}
